package model

import (
	"context"
	"fmt"

	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UserModel = (*customUserModel)(nil)

type (
	// UserModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUserModel.
	UserModel interface {
		userModel
		FindOneByUsername(ctx context.Context,username string)(*User,error)
		withSession(session sqlx.Session) UserModel
	}

	customUserModel struct {
		*defaultUserModel
	}
)

// NewUserModel returns a model for the database table.
func NewUserModel(conn sqlx.SqlConn) UserModel {
	return &customUserModel{
		defaultUserModel: newUserModel(conn),
	}
}

func (m *customUserModel) withSession(session sqlx.Session) UserModel {
	return NewUserModel(sqlx.NewSqlConnFromSession(session))
}

func (m *customUserModel)FindOneByUsername(ctx context.Context,username string)(*User,error){
	query := fmt.Sprintf("select %s from %s where username = $1 limit 1",userRows,m.table)
	var resp User
	err := m.conn.QueryRowCtx(ctx,&resp,query,username)
	fmt.Println("查看这里有没有查询成功","这个是username:"+username, err)
	switch err{
	case nil:
		return &resp,nil
	case sqlx.ErrNotFound:
		return nil,ErrNotFound
	default:
		return nil,err 
	}
}
